package com.jz.yanshe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jz.yanshe.eventbus.FragmentTabEvent;
import com.jz.yanshe.ui.EarningActivity;
import com.jz.yanshe.ui.FactoryActivity;
import com.jz.yanshe.ui.InviteActivity;
import com.jz.yanshe.ui.MessageActivity;
import com.jz.yanshe.ui.TeamActivity;
import com.jz.yanshe.ui.UploadVipFileActivity;
import com.jz.yanshe.util.ImageLoaderHelper;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.ui.ForNormalActivity;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.VIPBalanceResponse;
import com.jz.jd.rest.response.VIPDetailResponse;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.jz.yanshe.R;

/**
 * Created by Administrator on 2018/2/26.
 */

public class VipFragment extends BaseFragment {

    @BindView(R.id.tv_user_name)
    TextView tv_user_name;

    @BindView(R.id.tv_vip_id)
    TextView tv_vip_id;

    @BindView(R.id.img_vip_level)
    ImageView img_vip_level;

    @BindView(R.id.tv_yeji)
    TextView tv_yeji;

    @BindView(R.id.tv_shouyi)
    TextView tv_shouyi;

    @BindView(R.id.tv_team)
    TextView tv_team;

    @BindView(R.id.tv_all_team)
    TextView tv_all_team;

    @BindView(R.id.tv_parent_name)
    TextView tv_parent_name;


    @BindView(R.id.tv_balance)
    TextView tv_balance;


    @BindView(R.id.team_container)
    LinearLayout team_container;

    @BindView(R.id.img_level_2)
    ImageView img_level_2;
    @BindView(R.id.img_level_3)
    ImageView img_level_3;
    @BindView(R.id.img_level_4)
    ImageView img_level_4;
    @BindView(R.id.img_level_5)
    ImageView img_level_5;

    @BindView(R.id.img_avatar)
    ImageView img_avatar;

    @BindView(R.id.parent_container)
    LinearLayout parent_container;

    VIPDetailResponse.vipDetail vipDetail;


    public static VipFragment newInstance() {
        return new VipFragment();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_vip;

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    Context mContext;

    @Override
    public void onAttach(Context context) {
        mContext = context;
        super.onAttach(context);
    }

    @OnClick(R.id.btn_factory)
    void btn_factory() {
        startActivity(FactoryActivity.class);
    }

    private void getVipDetail() {
        RestProxy.getInstance().getVIPDetail(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<VIPDetailResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(VIPDetailResponse vipDetailResponse) {
//                PreferencesHelper.getInstance().set(getActivity(), "user_type", vipDetailResponse.map.user_type);
                if (vipDetailResponse.map.user_type == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("status", vipDetailResponse.map.confirmStatus.get(0).payment_credential_state);
                    bundle.putString("userType","2");
                    startActivity(UploadVipFileActivity.class, bundle);
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            EventBus.getDefault().post(new FragmentTabEvent("1"));
                        }
                    }, 200);
                    getActivity().finish();
                    return;
                }
                parent_container.setVisibility(View.VISIBLE);
                VipFragment.this.vipDetail = vipDetailResponse.map;
                PreferencesHelper.getInstance().set(getActivity(), "invite_code", vipDetailResponse.map.invite_code);
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(mContext, vipDetailResponse.map.head_url, img_avatar, R.mipmap.img_hot_default);
                tv_user_name.setText(vipDetailResponse.map.nickname);
                tv_vip_id.setText("ID：" + vipDetailResponse.map.vip_information_id);
                tv_yeji.setText(String.format("%.2f", Double.valueOf(vipDetailResponse.map.total_sales_price)));
                tv_shouyi.setText(String.format("%.2f", Double.valueOf(vipDetailResponse.map.total_earnings)));
                tv_all_team.setText("底层团队：" + vipDetailResponse.map.grade_user_num);
                tv_team.setText("直属团队：" + vipDetailResponse.map.directly_user_num);
                if (vipDetailResponse.map.pnickname != null && !vipDetailResponse.map.pnickname.equals("")) {
                    tv_parent_name.setText("邀请人：" + vipDetailResponse.map.pnickname);
                } else {
                    tv_parent_name.setText("邀请人：颜舍");
                }
                ImageLoaderHelper.getInstance().GlideImageLoader(mContext, vipDetailResponse.map.grade_icon, img_vip_level);
                switch (vipDetailResponse.map.vip_grade_id) {
                    case 5:
                        img_level_2.setVisibility(View.INVISIBLE);
                        img_level_3.setVisibility(View.INVISIBLE);
                        img_level_4.setVisibility(View.INVISIBLE);
                        img_level_5.setVisibility(View.INVISIBLE);
                        break;
                    case 4:
                        img_level_3.setVisibility(View.INVISIBLE);
                        img_level_4.setVisibility(View.INVISIBLE);
                        img_level_5.setVisibility(View.INVISIBLE);
                        break;
                    case 3:
                        img_level_4.setVisibility(View.INVISIBLE);
                        img_level_5.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        img_level_5.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        break;
                }

            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    EventBus.getDefault().post(new FragmentTabEvent("1"));
                    toLogin(((BusinessException) e).errorCode);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        parent_container.setVisibility(View.GONE);
        getVipDetail();
//        getBalance();
    }

    @OnClick(R.id.btn_my_earning)
    void btn_my_earning() {
        Bundle bundle = new Bundle();
        bundle.putSerializable("obj", vipDetail);
        if (vipDetail != null) {
            startActivity(EarningActivity.class, bundle);
        }
    }

    @OnClick(R.id.btn_invite)
    void btn_invite() {
        Bundle bundle = new Bundle();
        bundle.putSerializable("obj", vipDetail);
        if (vipDetail != null) {
            startActivity(InviteActivity.class, bundle);
        }
    }

    @OnClick(R.id.btn_message)
    void btn_message() {
        startActivity(MessageActivity.class);
    }


    @OnClick(R.id.btn_team)
    void btn_team() {
        if (team_container.getVisibility() == View.INVISIBLE) {
            team_container.setVisibility(View.VISIBLE);
        } else {
            team_container.setVisibility(View.INVISIBLE);
        }
    }

    @OnClick(R.id.btn_team_son)
    void tv_team() {
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        startActivity(TeamActivity.class, bundle);
    }

    @OnClick(R.id.btn_team_grandson)
    void btn_team_grandson() {
        Bundle bundle = new Bundle();
        bundle.putInt("type", 2);
        startActivity(TeamActivity.class, bundle);
    }


    private void getBalance() {
        RestProxy.getInstance().getVIPBalance(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<VIPBalanceResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(VIPBalanceResponse vipBalanceResponse) {
                tv_balance.setText("¥" + String.format("%.2f", vipBalanceResponse.balance));
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
